//页面加载完成
$(function () {
    var safetymechanism_datagrid = $("#safetymechanism_datagrid");

    var safetymechanism_form = $("#safetymechanism_form");

    var safetymechanism_dialog = $("#safetymechanism_dialog");

    //初始机构列表界面
    safetymechanism_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/safetymechanism/list',
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'name', title: '机构名称', width: 100},
            {field: 'code', title: '机构代码', width: 100},
            {field: 'legalPerson', title: '法人代表', width: 100},
            {field: 'dentityCard', title: '法人身份证', width: 100},
            {field: 'contactWay', title: '联系方式', width: 100},
            {field: 'address', title: '联系地址', width: 100},
            {field: 'cooperation', title: '合作状态', width: 100, formatter: stateFormatter}
        ]],
        toolbar: "#tb",
        onClickRow: function (index, row) {
            console.log(row);
            if (row.cooperation) {
                $("#btn_edit,#btn_changeState").linkbutton("enable");
            } else {
                $("#btn_edit,#btn_changeState").linkbutton("disable");
            }
        }
    })
    //将方法绑定到对象中
    var cmdObj = {
        add: function () {

            //清空form表单中的数据
            safetymechanism_form.form("clear");
            //设置title表单名
            safetymechanism_dialog.dialog("setTitle", "机构列表");
            //弹出对话框
            safetymechanism_dialog.dialog("open");
        },
        edit: function () {
            //选中一条需要删除的数据
            var row = safetymechanism_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲,请选择一条需要编辑的数据", "error");
                return;
            }
            //弹出对话框
            //设置title表单名
            safetymechanism_dialog.dialog("setTitle", "编辑列表");
            //弹出对话框
            safetymechanism_dialog.dialog("open");
            //清空form表单中的数据
            safetymechanism_form.form("clear");
            safetymechanism_form.form("load", row);
        },
        changeState: function () {

            var row = safetymechanism_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要解散的机构", "error");
                return;
            }
            //再次确认是否要解散
            $.messager.confirm("温馨提示", "亲,你确定解散该机构吗?", function (yes) {

                if (yes) {
                    //发送ajax请求
                    $.get("/safetymechanism/changeState?id=" + row.id, function (data) {

                        if (data.success) {
                            $.messager.alert("温馨提示", data.message, "info", function () {
                                cmdObj.reload();
                            })

                        } else {
                            $.messager.alert("温馨提示", data.message, "error");
                        }
                    });

                }
            });
        },
        reload: function () {
            safetymechanism_datagrid.datagrid("reload");
        },
        save: function () {
            var id = $("input[name='id']").val();
            var url;
            if (!id) {
                url = "/safetymechanism/save";
            } else {

                url = "/safetymechanism/edit"
            }
            //发送一条资源去访问后台
            safetymechanism_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (data.success) {
                        $.messager.alert("温馨提示", data.message, "info", function () {
                            cmdObj.cancel();
                            cmdObj.reload();
                        });
                    } else {

                        $.messager.alert("温馨提示", data.message, "error")
                    }
                }
            });
        },
        cancel: function () {
            safetymechanism_dialog.dialog("close");
        },
        search: function () {
            var keyword = $("#keyword").val();
            var stateId = $("#safetymechanism_select").combobox("getValue");
            console.log(keyword,stateId);

            $.post("/safetymechanism/list",{keyword:keyword,state:stateId},function (data) {
                safetymechanism_datagrid.datagrid("loadData",data);

            });


           /* safetymechanism_datagrid.datagrid("reload",{keyword:keyword,state:stateId});*/
        }
    }
    //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });

    //状态格式化回显
    //状态格式化回显
    function stateFormatter(value, row, index) {
        if (value == 0) {
            return "合作";
        } else if (value == 1) {
            return "<span style='color: red'>" + "解散" + "</span>";
        }

    }
        //初始化对话框
        $("#safetymechanism_dialog").dialog({
            width: 300,
            height: 400,
            closed: true,
            buttons: "#bb"
        })

})
